package cn.hdy.org.controller;

import cn.hdy.base.util.AjaxResult;
import cn.hdy.base.util.PageList;
import cn.hdy.org.domain.Department;
import cn.hdy.org.query.DepartmentQuery;
import cn.hdy.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/department")
@Api(value = "部门管理的CRUD", description = "部门管理的CRUD")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;

    @GetMapping
    @ApiOperation(value = "查询所有部门")
    public List<Department> findAll() {
        return departmentService.findAll();
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "查询单个部门")
    public Department findOne(@PathVariable("id") Long id) {
        return departmentService.findOne(id);
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除单个部门")
    public AjaxResult delete(@PathVariable("id") Long id) {

        departmentService.del(id);

        return AjaxResult.me();
    }

    @PatchMapping
    @ApiOperation(value = "批量删除部门")
    public AjaxResult delPatch(@RequestBody Long[] ids) {
        departmentService.patchDel(ids);

        return AjaxResult.me();
    }

    @PutMapping
    @ApiOperation(value = "添加或修改部门")
    public AjaxResult addOrUpdate(@RequestBody @Validated Department department) {

        if (department.getId() == null) {
            departmentService.add(department);
        } else {
            departmentService.update(department);
        }

        return AjaxResult.me();
    }

    @PostMapping
    @ApiOperation(value = "部门的分页查询")
    public PageList<Department> queryPage(@RequestBody DepartmentQuery departmentQuery) {
        return departmentService.queryPage(departmentQuery);
    }

    @GetMapping("/deptTree/{id}")
    @ApiOperation(value = "获取部门树")
    public List<Department> deptTree(@PathVariable("id") Long id) {
        return departmentService.deptTree(id);
    }
}
